using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.Windows.Forms;
using Betarium.PotonEditor.Properties;

namespace Betarium.PotonEditor
{
	public partial class PaletteForm : Form
	{

		public enum Pos
		{
			Main,
			Sub
		}

        private Color m_MainColor = Color.Black;
        private Color m_SubColor = Color.White;

        public Color SubColor
        {
            get { return m_SubColor; }
            set { m_SubColor = value; }
        }
	
        public Color MainColor
        {
            get { return m_MainColor; }
            set { m_MainColor = value; }
        }

        public MainForm MainForm
        {
            get { return (MainForm)Owner; }
        }

        public PaletteForm()
		{
			InitializeComponent();
		}

		private void ColorForm_Load(object sender, EventArgs e)
		{
			/*
			Bitmap image = new Bitmap(385, 64, PixelFormat.Format24bppRgb);
			Graphics graph = Graphics.FromImage(image);
			const int nShift = 6;
			const int nShiftDec = nShift - 1;
			const int nCellWidth = 4;
			const int nBlockWidth = 20;
			int[] aShift = new int[]{
				0,
				255 * 1 / 5,
				255 * 2 / 5,
				255 * 3 / 5,
				255 * 4 / 5,
				255,
			};
			for (int nY = 0; nY < nShift; nY++)
			{
				for (int nX = 0; nX < nShift; nX++)
				{
//					Color color = Color.FromArgb(255 - aShift[(nX + 1) * nY / nShift], aShift[nX], aShift[nShiftDec - nY]);
					Color color = Color.FromArgb(255, 255 - aShift[(nShift - nX) * nY / nShift], aShift[nShiftDec - nY]);
//					Color color = Color.FromArgb(255, aShift[nX * (nShift - nY) / nShift], aShift[nShiftDec - nY]);
					graph.FillRectangle(new SolidBrush(color), nX * nCellWidth, nY * nCellWidth, nCellWidth, nCellWidth);
				}
			}
			for (int nY = 0; nY < nShift; nY++)
			{
				for (int nX = 0; nX < nShift; nX++)
				{
					Color color = Color.FromArgb(255 - aShift[nX * (nY + 1) / nShift], 255, aShift[nShiftDec - nY]);
					//					image.SetPixel(nX + 64, nY, color);
					graph.FillRectangle(new SolidBrush(color), nX * nCellWidth + nBlockWidth, nY * nCellWidth, nCellWidth, nCellWidth);
				}
			}
			for (int nY = 0; nY < nShift; nY++)
			{
				for (int nX = 0; nX < nShift; nX++)
				{
					Color color = Color.FromArgb(aShift[nShiftDec - nY], 255, 255 - aShift[(nShift - nX) * nY / nShift]);
					//					image.SetPixel(nX + 128, nY, color);
					graph.FillRectangle(new SolidBrush(color), nX * nCellWidth + nBlockWidth * 2, nY * nCellWidth, nCellWidth, nCellWidth);
				}
			}
			for (int nY = 0; nY < nShift; nY++)
			{
				for (int nX = 0; nX < nShift; nX++)
				{
					Color color = Color.FromArgb(aShift[nShiftDec - nY], 255 - aShift[(nX + 1) * nY / nShift], 255);
					//					image.SetPixel(nX + 192, nY, color);
					graph.FillRectangle(new SolidBrush(color), nX * nCellWidth + nBlockWidth * 3, nY * nCellWidth, nCellWidth, nCellWidth);
				}
			}
			for (int nY = 0; nY < nShift; nY++)
			{
				for (int nX = 0; nX < nShift; nX++)
				{
					Color color = Color.FromArgb(255 - aShift[(nShift - nX) * nY / nShift], aShift[nShiftDec - nY], 255);
					//					image.SetPixel(nX + 256, nY, color);
					graph.FillRectangle(new SolidBrush(color), nX * nCellWidth + nBlockWidth * 4, nY * nCellWidth, nCellWidth, nCellWidth);
				}
			}
			for (int nY = 0; nY < nShift; nY++)
			{
				for (int nX = 0; nX < nShift; nX++)
				{
					Color color = Color.FromArgb(255, aShift[nShiftDec - nY], 255 - aShift[(nX + 1) * nY / nShift]);
					//					image.SetPixel(nX + 320, nY, color);
					graph.FillRectangle(new SolidBrush(color), nX * nCellWidth + nBlockWidth * 5, nY * nCellWidth, nCellWidth, nCellWidth);
				}
			}
			 * */
			/*
			for (int nY = 0; nY < nShift; nY++)
			{
				for (int nX = 0; nX < nShift; nX++)
				{
					Color color = Color.FromArgb(aShift[nShiftDec - nY], aShift[nX * (nShift - nY) / nShift], 0);
					graph.FillRectangle(new SolidBrush(color), nX * nCellWidth, nY * nCellWidth, nCellWidth, nCellWidth);
				}
			}
			for (int nY = 0; nY < nShift; nY++)
			{
				for (int nX = 0; nX < nShift; nX++)
				{
					Color color = Color.FromArgb(aShift[(nShiftDec - nX) * (nShift - nY) / nShift], aShift[nShiftDec - nY], 0);
					//					image.SetPixel(nX + 64, nY, color);
					graph.FillRectangle(new SolidBrush(color), nX * nCellWidth + nBlockWidth, nY * nCellWidth, nCellWidth, nCellWidth);
				}
			}
			for (int nY = 0; nY < nShift; nY++)
			{
				for (int nX = 0; nX < nShift; nX++)
				{
					Color color = Color.FromArgb(0, aShift[nShiftDec - nY], aShift[nX * (nShift - nY) / nShift]);
					//					image.SetPixel(nX + 128, nY, color);
					graph.FillRectangle(new SolidBrush(color), nX * nCellWidth + nBlockWidth * 2, nY * nCellWidth, nCellWidth, nCellWidth);
				}
			}
			for (int nY = 0; nY < nShift; nY++)
			{
				for (int nX = 0; nX < nShift; nX++)
				{
					Color color = Color.FromArgb(0, aShift[(nShiftDec - nX) * (nShift - nY) / nShift], aShift[nShiftDec - nY]);
					//					image.SetPixel(nX + 192, nY, color);
					graph.FillRectangle(new SolidBrush(color), nX * nCellWidth + nBlockWidth * 3, nY * nCellWidth, nCellWidth, nCellWidth);
				}
			}
			for (int nY = 0; nY < nShift; nY++)
			{
				for (int nX = 0; nX < nShift; nX++)
				{
					Color color = Color.FromArgb(aShift[nX * (nShift - nY) / nShift], 0, aShift[nShiftDec - nY]);
					//					image.SetPixel(nX + 256, nY, color);
					graph.FillRectangle(new SolidBrush(color), nX * nCellWidth + nBlockWidth * 4, nY * nCellWidth, nCellWidth, nCellWidth);
				}
			}
			for (int nY = 0; nY < nShift; nY++)
			{
				for (int nX = 0; nX < nShift; nX++)
				{
					Color color = Color.FromArgb(aShift[nShiftDec - nY], 0, aShift[(nShiftDec - nX) * (nShift - nY) / nShift]);
					//					image.SetPixel(nX + 320, nY, color);
					graph.FillRectangle(new SolidBrush(color), nX * nCellWidth + nBlockWidth * 5, nY * nCellWidth, nCellWidth, nCellWidth);
				}
			}
			 * */
			/*
			for (int nY = 0; nY < nShift; nY++)
			{
				for (int nX = 0; nX < nShift; nX++)
				{
					Color color = Color.FromArgb(aShift[nShiftDec - nY], aShift[nX * (nShift - nY) / nShift], 0);
					graph.FillRectangle(new SolidBrush(color), nX * 8, nY * 8, 8, 8);
				}
			}
			for (int nY = 0; nY < nShift; nY++)
			{
				for (int nX = 0; nX < nShift; nX++)
				{
					Color color = Color.FromArgb(aShift[(nShiftDec - nX) * (nShift - nY) / nShift], aShift[nShiftDec - nY], 0);
//					image.SetPixel(nX + 64, nY, color);
					graph.FillRectangle(new SolidBrush(color), nX * 8 + 64, nY * 8, 8, 8);
				}
			}
			for (int nY = 0; nY < nShift; nY++)
			{
				for (int nX = 0; nX < nShift; nX++)
				{
					Color color = Color.FromArgb(0, aShift[nShiftDec - nY], aShift[nX * (nShift - nY) / nShift]);
//					image.SetPixel(nX + 128, nY, color);
					graph.FillRectangle(new SolidBrush(color), nX * 8 + 128, nY * 8, 8, 8);
				}
			}
			for (int nY = 0; nY < nShift; nY++)
			{
				for (int nX = 0; nX < nShift; nX++)
				{
					Color color = Color.FromArgb(0, aShift[(nShiftDec - nX) * (nShift - nY) / nShift], aShift[nShiftDec - nY]);
//					image.SetPixel(nX + 192, nY, color);
					graph.FillRectangle(new SolidBrush(color), nX * 8 + 192, nY * 8, 8, 8);
				}
			}
			for (int nY = 0; nY < nShift; nY++)
			{
				for (int nX = 0; nX < nShift; nX++)
				{
					Color color = Color.FromArgb(aShift[nX * (nShift - nY) / nShift], 0, aShift[nShiftDec - nY]);
//					image.SetPixel(nX + 256, nY, color);
					graph.FillRectangle(new SolidBrush(color), nX * 8 + 256, nY * 8, 8, 8);
				}
			}
			for (int nY = 0; nY < nShift; nY++)
			{
				for (int nX = 0; nX < nShift; nX++)
				{
					Color color = Color.FromArgb(aShift[nShiftDec - nY], 0, aShift[(nShiftDec - nX) * (nShift - nY) / nShift]);
//					image.SetPixel(nX + 320, nY, color);
					graph.FillRectangle(new SolidBrush(color), nX * 8 + 320, nY * 8, 8, 8);
				}
			}
			*/
			/*
//			Graphics graph = Graphics.FromImage(image);
			Color baseColor = Color.GreenYellow;
			int nBaseR = baseColor.R;
			int nBaseG = baseColor.G;
			int nBaseB = baseColor.B;
			for (int nY = 0; nY < 64; nY++)
			{
				for (int nX = 0; nX < 64; nX++)
				{
					Color color = Color.FromArgb(nX * 4, nY * 4, 0);
//					graph.FillRectangle(new SolidBrush(color), nX, nY, 1, 1);
					image.SetPixel(nX, nY, color);
				}				
			}
			for (int nY = 0; nY < 64; nY++)
			{
				for (int nX = 0; nX < 64; nX++)
				{
					Color color = Color.FromArgb(252 - nX * 4, nY * 4, nX * 4);
					image.SetPixel(nX + 64, nY, color);
				}
			}
			graph.Dispose();
			*/
//			ctrlPalette.Image = image;
//			ctrlPalette.Image = Properties.Resources.palette;
			/*
			Bitmap bmpMain = new Bitmap(32, 32, PixelFormat.Format24bppRgb);
			using(Graphics grpMain = Graphics.FromImage(bmpMain))
			{
				grpMain.FillRectangle(new SolidBrush(Color.Black), 0, 0, bmpMain.Width, bmpMain.Height);
			}
			ctrlMainColor.Image = bmpMain;
			 * */
			SetColor(Pos.Main, Color.Black);
			SetColor(Pos.Sub, Color.White);
		}

		public void SetColor(Pos pos, Color nColor)
		{
			Bitmap bmpMain = new Bitmap(32, 32, PixelFormat.Format24bppRgb);
			using (Graphics grpMain = Graphics.FromImage(bmpMain))
			{
				grpMain.FillRectangle(new SolidBrush(nColor), 0, 0, bmpMain.Width, bmpMain.Height);
			}
			if (pos == Pos.Main)
			{
                m_MainColor = nColor;
                ctrlMainColor.Image = bmpMain;
			}
			else
			{
                m_SubColor = nColor;
                ctrlSubColor.Image = bmpMain;
			}
		}

        public void SetViewColor(Pos pos, Color nColor)
        {
            if (!(MainForm.ActiveMdiChild is EditViewForm))
            {
                return;
            }
            EditViewForm viewForm = (EditViewForm)MainForm.ActiveMdiChild;
            if(pos == Pos.Main)
                viewForm.PaintState.MainColor = nColor;
            else if (pos == Pos.Sub)
                viewForm.PaintState.SubColor = nColor;
            viewForm.OnPaintStateChanged();
        }

        private void ctrlPalette_MouseClick(object sender, MouseEventArgs e)
		{
			Bitmap image = Resources.palette;
			Color color = image.GetPixel(e.X, e.Y);
			MainForm mainForm = (MainForm)Owner;
			if (e.Button == MouseButtons.Left)
			{
				SetColor(Pos.Main, color);
				if (!(mainForm.ActiveMdiChild is EditViewForm))
				{
					return;
				}
                SetViewColor(Pos.Main, color);
//                EditViewForm viewForm = (EditViewForm)mainForm.ActiveMdiChild;
//				viewForm.PaintState.MainColor = color;
//                viewForm.OnPaintStateChanged();
			}
			if (e.Button == MouseButtons.Right)
			{
				SetColor(Pos.Sub, color);
				if (!(mainForm.ActiveMdiChild is EditViewForm))
				{
					return;
				}
                SetViewColor(Pos.Sub, color);
//				EditViewForm viewForm = (EditViewForm)mainForm.ActiveMdiChild;
//				viewForm.PaintState.SubColor = color;
//                viewForm.OnPaintStateChanged();
            }

		}

		private void PaletteForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (e.CloseReason == CloseReason.UserClosing)
			{
				Visible = false;
				e.Cancel = true;
				MainForm owner = (MainForm)Owner;
				owner.menuViewShowPalette.Checked = false;
			}
		}

        private void ctrlPalette_Click(object sender, EventArgs e)
        {

        }


        private void ctrlMainColor_DoubleClick(object sender, EventArgs e)
        {
            //2008/07/16 
            ColorDialog dialog = new ColorDialog();
            dialog.Color = m_MainColor;
            if (dialog.ShowDialog() != DialogResult.OK)
                return;
            SetColor(Pos.Main, dialog.Color);
            SetViewColor(Pos.Main, dialog.Color);
        }

        private void ctrlSubColor_DoubleClick(object sender, EventArgs e)
        {
            //2008/07/16 
            ColorDialog dialog = new ColorDialog();
            dialog.Color = m_SubColor;
            if (dialog.ShowDialog() != DialogResult.OK)
                return;
            SetColor(Pos.Sub, dialog.Color);
            SetViewColor(Pos.Sub, dialog.Color);
        }
	}
}